package kyssion.leetcode.num201_250;

import kyssion.leetcode.util.ListNode;

public class code234_回文列表 {

    public static void main(String[] args) {
        System.out.println(new code234_回文列表().isPalindrome(
                ListNode.createListNode(new int[]{
                        1,2
                })
        ));
    }

    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode one = head;
        ListNode two = head.next;
        while (two != null && two.next != null) {
            one = one.next;
            two = two.next.next;
        }
        two = one;
        ListNode headItem = one;
        while (one != null) {
            ListNode item = one.next;
            one.next = headItem;
            headItem = one;
            one = item;
        }
        two.next = null;
        one = head;
        two = headItem;
        while (one != null && two != null) {
            if (one.val != two.val) {
                return false;
            } else {
                one = one.next;
                two = two.next;
            }
        }
        return true;
    }
}
